package com.jade.shoesmallinterface.controller;

import com.jade.shoesmallinterface.entity.Product;
import com.jade.shoesmallinterface.entity.Result;
import com.jade.shoesmallinterface.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shoes/product")
public class ProductController {
    @Autowired
    private ProductService productService;

    /**
     * 查询所有商品信息
     */
    @GetMapping("/selectAllProducts")
    public Result<List<Product>> selectAllProducts() {
        try {
            List<Product> products = productService.selectAllProducts();
            return Result.success(products);
        } catch (Exception e) {
            System.out.println("查询商品出错: " + e);
            return Result.error("查询商品出错");
        }
    }

    /**
     * 根据ID查询商品信息
     */
    @GetMapping("/selectProductById")
    public Result<Product> selectProductById( Integer productId) {
        if (productId == null) {
            return Result.error("商品ID不能为空");
        }
        Product product = productService.selectProductById(productId);
        return Result.success(product);
    }


    /**
     * 根据品牌或商品名称查询商品
     */
    @GetMapping("/selectProductsByBrandOrName")
    public Result<List<Product>> selectProductsByBrandOrName(
            @RequestParam(required = false) String brand,
            @RequestParam(required = false) String productName
    ) {
        try {
            List<Product> products = productService.selectProductsByBrandOrName(brand, productName);
            return Result.success(products);
        } catch (Exception e) {
            System.out.println("查询商品出错: " + e);
            return Result.error("查询商品出错");
        }
    }
}
